﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;
using TAM_SPORT;

namespace AMS
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
            this.BackColor = Color.LightCyan;
        }

        private void choThuêPhòngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmThuePhong f = new FrmThuePhong();
            f.MdiParent = this;
            f.Show();
        }

        private void danhSáchPhòngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmPhong f = new FrmPhong();
            f.MdiParent = this;
            f.Show();
        }

        private void danhSachXe_Click(object sender, EventArgs e)
        {
            FrmMoto f = new FrmMoto();
            f.MdiParent = this;
            f.Show();
        }

        private void danhSáchTrangThiêcBịToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmThietBi f = new FrmThietBi();
            f.MdiParent = this;
            f.Show();
        }

        private void danhSáchThuêPhòngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmThuePhongList f = new FrmThuePhongList();
            f.MdiParent = this;
            f.Show();
        }

        private void Main_Shown(object sender, EventArgs e)
        {
            Login f = new Login();
            f.ShowDialog();
            if (Login.userName == null || Login.userName.Equals(""))
            {
                this.Dispose();
            }
            else
            {
                if (LoginDao.adminFlag)
                {
                    thêmNgườiDùngToolStripMenuItem.Visible = true;
                    cẤUHÌNHToolStripMenuItem.Visible = true;
                }
                else
                {
                    thêmNgườiDùngToolStripMenuItem.Visible = false;
                    cẤUHÌNHToolStripMenuItem.Visible = false;
                }
            }
            openCustomer();
        }

        private void openCustomer()
        {
            try
            {
                FrmThuePhongList cus = new FrmThuePhongList();
                cus.MdiParent = this;
                cus.Show();
                cus.WindowState = FormWindowState.Minimized;
                cus.WindowState = FormWindowState.Maximized;
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
                Application.Exit();
            }
        }

        private void chiTiêuToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FrmChiTieu f = new FrmChiTieu();
            f.MdiParent = this;
            f.Show();
        }

        private void danhSáchChiTiêuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmChiTieuList f = new FrmChiTieuList();
            f.MdiParent = this;
            f.Show();
        }

        private void thêmNgườiDùngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewUser ne = new NewUser();
            ne.MdiParent = this;
            ne.Show();
        }

        private void thốngKêToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FrmReport report = new FrmReport();
            report.MdiParent = this;
            report.Show();
        }

        private void danhSáchPhiếuThuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmDanhSachPhieuThu danhSachPhieuThu = new FrmDanhSachPhieuThu();
            danhSachPhieuThu.MdiParent = this;
            danhSachPhieuThu.Show();
        }

        private void cẤUHÌNHToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmConfig f = new FrmConfig();
            f.ShowDialog();
            f.Dispose();
        }

        private void txtQuickSearch_Enter(object sender, EventArgs e)
        {
            txtQuickSearch.Text = "";
            txtQuickSearch.TextBoxTextAlign = HorizontalAlignment.Left;
        }

        private void minimizeOnpeningForm()
        {
            List<Form> formsFromOtherAssemblies = new List<Form>();
            foreach (Form form in Application.OpenForms)
            {
                if (form.Name != "Main")
                {
                    formsFromOtherAssemblies.Add(form);
                }
            }
            for (int i = 0; i < formsFromOtherAssemblies.Count; i++)
            {
                formsFromOtherAssemblies[i].Close();
            }
        }

        private void txtQuickSearch_KeyDown(object sender, KeyEventArgs e)
        {
            string str = txtQuickSearch.Text;
            if (e.KeyCode == Keys.Enter)
            {
                minimizeOnpeningForm();
                FrmThuePhongList f = new FrmThuePhongList(str);
                f.MdiParent = this;
                f.Show();
                f.WindowState = FormWindowState.Minimized;
                f.WindowState = FormWindowState.Maximized;
                txtQuickSearch.TextBoxTextAlign = HorizontalAlignment.Center;
                txtQuickSearch.Text = "Tìm nhanh";
            }   
        }

        private void txtQuickSearch_Leave(object sender, EventArgs e)
        {
            txtQuickSearch.TextBoxTextAlign = HorizontalAlignment.Center;
            txtQuickSearch.Text = "Tìm nhanh";
        }

        private void logOutClickAction(object sender, EventArgs e)
        {
            minimizeOnpeningForm();
            Main_Shown(null, null);
        }

        private void exitClickAction(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
